package com.prototype.demo02;

import java.io.*;

/**
 * 对象的流式写出和写入是深复制操作,因此,
 * Client必须实现Serializable接口,否则不能深复制
 */
public class Client {
    public static void main(String[] args) throws Exception {
        Client client = new Client();
        Object o = client.deepClone();
        System.out.println(o);
    }

    public Object deepClone() throws Exception {
        // 将对象写到流里
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream oo = new ObjectOutputStream(bo);
        oo.writeObject(this);

        // 从流里读回来
        ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
        ObjectInputStream oi = new ObjectInputStream(bi);
        return oi.readObject();
    }
}
